﻿<%@ Page Title="" Language="C#" MasterPageFile="~/master/m.master" %>

<script runat="server"></script>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <link href="../assets/css/plugins/footable/footable.core.css" rel="stylesheet">
    <link href="../assets/css/replenishment/intervalSetting.css" rel="stylesheet">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="main" runat="Server">
    <div class="system-box">
        <div class="right-side">
            <div class="btns-container"></div>
            <div class="ibox-content">
                <div id="goodsSetting" class="setting">
                    <div class="settingTitle">商品层设置</div>
                    <div class="settingContent">
                        <table id="goodsTable" class="footable table table-stripped">
                            <thead></thead>
                            <tbody id="goodsList">
                            </tbody>
                        </table>
                    </div>
                </div>
                <div id="lifeSetting" class="setting">
                    <div class="settingTitle">生命周期区间设置</div>
                    <div class="settingContent">
                        <table id="lifeTable" class="footable table table-stripped">
                            <thead></thead>
                            <tbody id="lifeList"></tbody>
                        </table>
                    </div>
                </div>
                <div id="otherSetting" class="setting">
                    <div class="settingTitle">爆畅平滞区间设置</div>
                    <div class="settingContent">
                        <table id="otherTable" class="footable table table-stripped">
                            <thead></thead>
                            <tbody id="otherList"></tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="footer" runat="Server">
    <script src="../assets/js/plugins/footable/footable.all.min.js"></script>
    <script src="../assets/js/page/tableControl.js"></script>
    <script src="../assets/js/page/rScroll.js"></script>
    <script type="application/javascript">
        $(document).ready(function () {
            //模块ID
            var ModuleID = Y.getUrlParam("ModuleID");
            //当前页数
            var page = 1;
            //获取按钮
            commonUtil.loadBtnList(ModuleID, function () {
                rScroll();
            });
            //初始化表头
            tableControl.initThead2('#goodsTable', ['序号', '生命周期']);
//          tableControl.initThead2('#lifeTable', ['开始天数', '结束天数', '区间名称', ' ']);
			tableControl.initThead2('#lifeTable', ['开始天数', '结束天数', '区间名称']);
            tableControl.initThead2('#otherTable', ['类型', '开始值', '结束值']);
            var tempArr = [];
            //生命周期区间设置
            var lifeData = [];
            //
            var otherData = [];
            //全局变量
            var lifeBegin = 1;	//生命周期开始值
            var lifeEnd = 1;	//生命周期结束值

            var GetList = function () {
                Y.API2('DwGlobalVariable', 'GetGlobalVariableList', { dictValue: '生命周期' }, function (data) {
                    
                    var _html = "";
                    var _index = 1;
                    data.map(function (item, i) {
                        if (item.dictValue != "生命周期") {
                            _html += '<tr><td class="hide">' + item.dictValue + '</td><td>' + _index + '</td><td>' + item.dictValue + '</td></tr>';
                            _index++;
                        }
                    })
                    $("#goodsList").html(_html);
                    $("#goodsList tr:first").click();
                })
            }
            GetList();
            //获取商品层信息
            $('#goodsList').on('click', 'tr', function () {
                $('#goodsList tr').removeClass('check');
                $(this).addClass('check');
                //爆畅滞平初始化
                $('#otherList').empty();
                //生命周期初始化
                $('#lifeList').empty();
                lifeEnd = parseInt($(this).children().eq(2).text());
                //临时数组,将要替换成api获取结果
                layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
                Y.API2('DwCZPJudgePeriod', 'GetLifeCycleList', { dayNum: lifeEnd}, function (data) {
                	layer.closeAll('dialog');
                	if(data.Success == false){
                		swal(data.Message,'','error');
                		return false;
                	}
                		
                    lifeData = data;
                    var tBody = [];
                    if (lifeData.length < 2) {
                        var tempTr = {
                            trClass: '',
                            children: [{ tdText: 1, c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'hide' }, ]
                        };
                        tempTr.children.push({ tdText: lifeBegin, c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'beginDay' });
                        if (lifeEnd != 1) {
//                          tempTr.children.push({ tdText: '', c: 0, r: 0, tdHtml: '<input type="number" class="endDay" value="' + lifeEnd + '" />', tdId: '', tdClass: '' });
							tempTr.children.push({ tdText: lifeEnd, c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' });
                            tempTr.children.push({ tdText: lifeBegin + '-' + lifeEnd + '天', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'trdTd' });
//                          tempTr.children.push({ tdText: '', c: 0, r: 0, tdHtml: '<i class="fa fa-plus"></i>', tdId: '', tdClass: '' });
                        } else {
                            tempTr.children.push({ tdText: '1', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' });
                            tempTr.children.push({ tdText: lifeEnd + '天', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' });
//                          tempTr.children.push({ tdText: ' ', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' });
                        }
                        tBody.push(tempTr);
                    } else {
                        lifeData.map(function (item, i) {
                            var tempTr = {
                                trClass: (i % 2 == 0 ? 'gradeX footable-even' : 'gradeX footable-odd'),
                                children: [{ tdText: 1, c: 0, r: 0, tdHtml: '', tdId: item.LifeDays, tdClass: 'hide' }, ]
                            };
                            tempTr.children.push({ tdText: item.BeginDay, c: 0, r: 0, tdHtml: '', tdId: item.LifeDays, tdClass: 'beginDay' });
//                          tempTr.children.push({ tdText: '', c: 0, r: 0, tdHtml: '<input type="number" class="endDay" value="' + item.EndDay + '" />', tdId: item.LifeDays, tdClass: '' });
							tempTr.children.push({ tdText: item.EndDay, c: 0, r: 0, tdHtml: '', tdId: item.LifeDays, tdClass: '' });
                            tempTr.children.push({ tdText: item.PeriodName, c: 0, r: 0, tdHtml: '', tdId: item.LifeDays, tdClass: 'trdTd' });
                            //tempTr.children.push({ tdText: '', c: 0, r: 0, tdHtml: '<i class="fa fa-plus"></i>', tdId: item.LifeDays, tdClass: '' });

                            //if (item.EndDay != item.LifeDays)
                            //    tempTr.children.push({ tdText: '', c: 0, r: 0, tdHtml: '<i class="fa fa-minus"></i>', tdId: item.LifeDays, tdClass: '' });
                            //else {
                            //    if (item.EndDay == item.LifeDays)
                            //        tempTr.children.push({ tdText: '', c: 0, r: 0, tdHtml: '', tdId: item.LifeDays, tdClass: '' });
                            //    else
                            //        tempTr.children.push({ tdText: '', c: 0, r: 0, tdHtml: '<i class="fa fa-plus"></i>', tdId: item.LifeDays, tdClass: '' });
                            //}
//                          tempTr.children.push({ tdText: '', c: 0, r: 0, tdHtml: '<i class="fa fa-plus"></i>', tdId: item.LifeDays, tdClass: '' });
                            tBody.push(tempTr);
                        })
                    }

                    //		var tempBeginDay = 1;
                    //		var tempEndDay = parseInt($(this).children().eq(2).text());
                    //		tempTr.children.push({tdText: '', c:0 , r:0, tdHtml:'<input type="number" class="beginDay" value="'+ tempBeginDay +'" />', tdId:'', tdClass:''});
                    //		tempTr.children.push({tdText: '', c:0 , r:0, tdHtml:'<input type="number" class="endDay" value="'+ tempEndDay +'" />', tdId:'', tdClass:''});
                    //		tempTr.children.push({tdText: tempBeginDay + '-' + tempEndDay + '天', c:0 , r:0, tdHtml:'', tdId:'', tdClass:''});
                    tableControl.initTbody('#lifeTable', (tBody.length > 0 ? tBody : null), 2, null);
                    $('#lifeList .fa-plus').addClass('fa-minus').removeClass('fa-plus');
                    //如果达到结束临界
                    if (parseInt($('#lifeList .beginDay:last').text()) >= lifeEnd - 2)
                        $('#lifeList .fa-plus').hide();
                    else {
                        $('#lifeList .fa-minus:last').addClass('fa-plus').removeClass('fa-minus');
                    }
                    
                    $('#lifeList tr:first').click();
                })
            });

            //添加生命周期区间
            $('#lifeList').on('click', '.fa-plus', function () {
                //获取本行容器
                var tr = $(this).parents('tr');
                var nextTr = tr.clone(true);
                //获取本行开始值
                var beginDay = parseInt(tr.find('.beginDay').text());
                var endDay = beginDay + 1;
                //修改本行结束值
                tr.find('.endDay').val(endDay);
                tr.find('.trdTd').text(beginDay + '-' + endDay + '天');
                //修改本按钮
                $(this).addClass('fa-minus').removeClass('fa-plus');
                //处理下一行
                $('#lifeList').append(nextTr);
                tr.next().find('.beginDay').text(endDay + 1);
                tr.next().find('.trdTd').text((endDay + 1) + '-' + lifeEnd + '天');
                //如果达到结束临界
                if (parseInt($('#lifeList .beginDay:last').text()) >= lifeEnd - 2)
                    $('#lifeList .fa-plus').hide();
                $('#lifeList tr').removeClass('check');
               
                updateLife();
            })

            //删除生命周期区间
            $('#lifeList').on('click', '.fa-minus', function () {
                //获取本行容器
                var tr = $(this).parents('tr');
                var beginDay = parseInt(tr.find('.beginDay').text());
                var endDay = parseInt(tr.next().find('.endDay').val());
                tr.next().find('.beginDay').text(beginDay);
                tr.next().find('.trdTd').text(beginDay + '-' + endDay + '天');
                tr.remove();
                $('#lifeList .fa-plus').show();
                updateLife();
            });

            //修改生命周期结束天数
            $('#lifeList').on('blur', '.endDay', function () {
                $('#lifeList tr').removeClass('check');
                //获取本输入框下标
                var eIndex = $('#lifeList .endDay').index(this);
                //不允许修改最大生命周期
                if (eIndex >= $('#lifeList .endDay').size() - 1) {
                    $(this).val(lifeEnd);
                    return false;
                }

                //获取本行容器
                var tr = $(this).parents('tr');
                //本行开始值
                var beginDay = parseInt(tr.find('.beginDay').text());
                //判断输入值
                var endDay = parseInt($(this).val());
                var check = true;
                if (endDay < lifeBegin + 1) {
                    swal('结束天数至少为' + (lifeBegin + 1));
                    check = false;
                }
                if (endDay >= lifeEnd - 1) {
                    swal('输入的结束天数必须小于' + (lifeEnd - 1));
                    check = false;
                }
                if (isNaN(endDay))
                    check = false;
                //复原输入值
                if (!check) {
                    $(this).val(parseInt(tr.next().find('.beginDay').text()) - 1);
                    return false;
                }
                $(this).val(endDay);
                //判断处理方向
                if (endDay > beginDay) {
                    //往下处理
                    //tr.next().find('.beginDay').text(endDay + 1);
                    var count = $('#lifeList .endDay').size() - 1;
                    for (var i = eIndex + 1; i <= count; i++) {
                        var tempTr = $('#lifeList tr').eq(i);
                        var tempBegin = parseInt(tempTr.find('.beginDay').text());
                        var tempEnd = parseInt(tempTr.find('.endDay').val());
                        if (tempEnd == lifeEnd) {
                            tempTr.find('.beginDay').text(endDay + 1);
                            $('#lifeList .fa-plus').hide();
                            break;
                        }
                        if (endDay >= tempEnd) {
                            tempTr.find('.endDay').val(endDay);
                        }
                        if (endDay > tempBegin) {
                            tempTr.find('.beginDay').text(endDay);
                        } else {
                            if (tempBegin == endDay) {
                                tempTr.find('.endDay').val(endDay);
                                var pEnd = parseInt(tempTr.next().find('.beginDay').text());
                                tempTr.next().find('.beginDay').text(pEnd - 1);
                            }
                            break;
                        }
                    }
                }
                else {
                    tr.next().find('.beginDay').text(endDay + 1);
                    //往上处理
                    for (var i = eIndex; i >= 0; i--) {
                        var tempTr = $('#lifeList tr').eq(i);
                        var tempBegin = parseInt(tempTr.find('.beginDay').text());
                        var tempEnd = parseInt(tempTr.find('.endDay').val());
                        if (tempEnd >= endDay)
                            tempTr.find('.endDay').val(endDay);
                        else
                            break;

                        if (tempBegin > endDay) {
                            tempTr.find('.beginDay').text(endDay);
                        } else {
                            if (tempBegin == endDay) {
                                var pEnd = parseInt(tempTr.prev().find('.endDay').val());
                                tempTr.prev().find('.endDay').val(pEnd + 1);
                            }
                            break;
                        }
                    }
                }
                //重新整理
                $('#lifeList tr').each(function () {
                    if (parseInt($(this).find('.beginDay').text()) == parseInt($(this).find('.endDay').val())) {
                        $(this).remove();
                        $('#lifeList .fa-plus').show();
                    } else {
                        $(this).find('.trdTd').text($(this).find('.beginDay').text() + '-' + $(this).find('.endDay').val() + '天');
                    }
                });
                //如果达到结束临界
                if (parseInt($('#lifeList .beginDay:last').text()) >= lifeEnd - 2)
                    $('#lifeList .fa-plus').hide();
            })

            //选择生命周期区间
            $('#lifeList').on('click', 'tr', function () {
                $('#lifeList tr').removeClass('check');
                $(this).addClass('check');
                var periodName = $(this).children().eq(3).text();
                //加载爆畅滞平区间数据
                $('#otherList').empty();
                var tBody = [];
                var arrList = [
                        {
                            trClass: '',
                            children: [
                                { tdText: periodName, c: 0, r: 0, tdHtml: '', tdId: periodName, tdClass: 'hide tID' },
                                { tdText: '爆', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'saleFlag' },
                                { tdText: '', c: 0, r: 0, tdHtml: '<input type="number" class="beginDay" value="0" />', tdId: '', tdClass: '' },
                                 { tdText: '', c: 0, r: 0, tdHtml: '<input type="number" class="endDay" disabled value="0" />', tdId: '', tdClass: '' }
                            ]
                        },
                        {
                            trClass: '',
                            children: [
                                { tdText: periodName, c: 0, r: 0, tdHtml: '', tdId: periodName, tdClass: 'hide tID' },
                                { tdText: '畅', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'saleFlag' },
                                { tdText: '', c: 0, r: 0, tdHtml: '<input type="number" class="beginDay" value="0" />', tdId: '', tdClass: '' },
                                { tdText: '', c: 0, r: 0, tdHtml: '<input type="number" class="endDay" disabled value="0" />', tdId: '', tdClass: '' }
                            ]
                        },
                        {
                            trClass: '',
                            children: [
                                { tdText: periodName, c: 0, r: 0, tdHtml: '', tdId: periodName, tdClass: 'hide tID' },
                                { tdText: '平', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'saleFlag' },
                                { tdText: '', c: 0, r: 0, tdHtml: '<input type="number" class="beginDay" value="0" />', tdId: '', tdClass: '' },
                                { tdText: '', c: 0, r: 0, tdHtml: '<input type="number" class="endDay" disabled value="0" />', tdId: '', tdClass: '' }
                            ]
                        },
                        {
                            trClass: '',
                            children: [
                                { tdText: periodName, c: 0, r: 0, tdHtml: '', tdId: periodName, tdClass: 'hide tID' },
                                { tdText: '滞', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'saleFlag' },
                                { tdText: '', c: 0, r: 0, tdHtml: '<input type="number" class="beginDay" value="0" />', tdId: '', tdClass: '' },
                                { tdText: '', c: 0, r: 0, tdHtml: '<input type="number" class="endDay" disabled value="0" />', tdId: '', tdClass: '' }
                            ]
                        },
                        {
                            trClass: '',
                            children: [
                                { tdText: periodName, c: 0, r: 0, tdHtml: '', tdId: periodName, tdClass: 'hide tID' },
                                { tdText: '超滞', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'saleFlag' },
                                { tdText: '', c: 0, r: 0, tdHtml: '<input type="number" class="beginDay" value="0" />', tdId: '', tdClass: '' },
                                { tdText: '', c: 0, r: 0, tdHtml: '<input type="number" class="endDay" disabled value="0" />', tdId: '', tdClass: '' }
                            ]
                        }
                ];
                if (lifeData.length != 0) {
                    lifeData.map(function (item) {
                        if (item.PeriodName == periodName) {
                            if (item.CZPJudgePeriodList.length == 0) {
                                tBody = arrList;
                            } else {
                                item.CZPJudgePeriodList.map(function (subItem) {
                                    var tempTr = {
                                        trClass: '',
                                        children: [{ tdText: periodName, c: 0, r: 0, tdHtml: '', tdId: periodName, tdClass: 'hide tID' }, ]
                                    };
                                    tempTr.children.push({ tdText: subItem.SaleFlag, c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'saleFlag' });
                                    tempTr.children.push({ tdText: '', c: 0, r: 0, tdHtml: '<input type="number" class="beginDay" value="' + subItem.BeginValue + '" />', tdId: '', tdClass: '' });
                                    tempTr.children.push({ tdText: '', c: 0, r: 0, tdHtml: '<input type="number" class="endDay" disabled value="' + subItem.EndValue + '" />', tdId: '', tdClass: '' });
                                    tBody.push(tempTr);
                                })
                            }
                        }
                    });
                } else {
                    tBody = arrList;
                }
                tableControl.initTbody('#otherTable', (tBody.length > 0 ? tBody : null), 2, null);
                $('#otherList .beginDay:last').attr('disabled', 'disabled');
                lifeData.map(function (item) {
                    if (item.PeriodName == periodName) {
                        if (item.CZPJudgePeriodList.length == 0)
                            updateOther();
                    }
                })
                
            })
            var updateOther = function () {
                var tempArr = [];
                var tID = '';
                $('#otherList tr').each(function (i) {
                    var beginDay = parseInt($(this).find('.beginDay').val());
                    var endDay = parseInt($(this).find('.endDay').val());
                    var saleFlag = $(this).find('.saleFlag').text();
                    tID = $(this).find('.tID').text();
                    tempArr.push({
                        SaleFlag: saleFlag,
                        BeginValue: beginDay,
                        EndValue: endDay,
                        SortId: i + 1
                    });
                });
                lifeData.map(function (item) {
                    if (item.PeriodName == tID) {
                        //先删除再添加
                        //item.CZPJudgePeriod.splice(i, 1);
                        item.CZPJudgePeriodList = [];
                        item.CZPJudgePeriodList = tempArr;
                    }
                })
            }

            //修改开始值
            $('#otherList').on('blur', '.beginDay', function () {
                var eIndex = $('#otherList .beginDay').index(this);
                console.log(eIndex);
                //获取本行容器
                var tr = $(this).parents('tr');
                //本行开始值
                var beginDay = parseInt($(this).val());
                //判断输入值                
                var endDay = parseInt(tr.find('.endDay').val());
                if (eIndex > 0) {
                    if (beginDay > endDay) {
                        tr.find('.beginDay').val(0);
                        return;
                    }
                }
                tr.next().find('.endDay').val(beginDay);
                updateOther();
            });
            //修改结束值
            $('#otherList').on('blur', '.endDay', function () {
                //获取本行容器
                var tr = $(this).parents('tr');
                //本行开始值
                var beginDay = parseInt(tr.find('.beginDay').val());
                //判断输入值
                var endDay = parseInt($(this).val());

            });
            var updateLife = function () {
                lifeData = [];
                $('#lifeList tr').each(function () {
                    var beginDay = parseInt($(this).find('.beginDay').text());
                    var endDay = parseInt($(this).find('.endDay').val());
                    var periodName = $(this).find('.trdTd').text();
                    lifeData.push({ LifeDays: lifeEnd, PeriodName: periodName, BeginDay: beginDay, EndDay: endDay, CZPJudgePeriodList: [] });
                });
            }
            //保存按钮
            $('.btns-container').on('click', '#btn-save', function () {
                console.log(lifeData);
                var isCheck = false;
                lifeData.map(function (item) {
                    if (item.CZPJudgePeriodList.length == 0) {
                        swal(item.PeriodName+'数据未设置', '', 'error');
                        isCheck = true;
                        return;
                    }
                })
                if (isCheck) return;
                
				layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
                Y.API2('DwCZPJudgePeriod', 'ModifyLifeCycle', { LifeDays: lifeEnd, LifeCycleInfo: lifeData }, function (data) {
                	layer.closeAll('dialog');
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        swal('保存成功！', '', 'success');
                    }
                })
            })
        })
    </script>
</asp:Content>

